<?php

/**
 * 
 * Log Resource
 * 
 * <code>
 * ; Example application.ini settings
 * resources.log.writer.1.type = file
 * resources.log.writer.1.path = APPLICATION_PATH "/data/logs/"
 *
 * ; Add mail logging on log entries with ERR level priority or higher
 * resources.log.writer.1.type = mail
 * resources.log.writer.1.from = webmaster@domain.com
 * resources.log.writer.1.to = developer@domain.com
 * resources.log.writer.1.subject = "Log %s"
 * resources.log.writer.1.level = ERR
 * </code>
 * 
 *
 * @filesource
 * @author Michael <webmaster@webjestic.net>
 * @link http://webjestic.net webJestic
 * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
 * @copyright Copyright (c) 2009.  All Rights Reserved
 * 
 * @category Engine
 * @package  Resources
 * @subpackage Log
 */

class Engine_Resource_Log extends Zend_Application_Resource_ResourceAbstract {
	
	protected $_log;
	
	public function init() {
		return $this->getLog();
	}
	
	public function getLog() {
		if (null === $this->_log) {
			
			// Load options for log
			$options = $this->getOptions();
			$this->_log = new Zend_Log();
			$this->_log->setTimestampFormat("Y-m-d H:i:s");
			
			// Spin through the different writers and add them
			if (isset($options['writer'])) {
				foreach ($options['writer'] as $writer) {
					switch ($writer['type']) {
						
						// Create the Writers for each log type
						case 'file' :
							$path = isset($writer['path']) ? $writer['path']: APPLICATION_PATH . "/data/logs/";
							$file = APPLICATION_ENV . '_' . date('Y.m.d'). '.log';
							$writer = new Zend_Log_Writer_Stream($path.$file);
							break;
						default :
							$writer = new Zend_Log_Writer_Null(); 
							break;
					}
					$this->_log->addWriter($writer);
				}
			}
		}
		Zend_Registry::set('Zend_Log', $this->_log);
		return $this->_log;	
	}
	
}

?>